package at.fhj.routenplaner;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

public class SettingsActivity extends Activity implements OnClickListener {
    /** Called when the activity is first created. */
    
	Button btnSave, btnLoad;
	EditText edStreet, edCity;
	RadioGroup radioGroup, radioGroup2;
	RadioButton radioBtn1, radioBtn2, radioBtn3, radioBtn4;
	CheckBox checkBox1;
	int geklickterButton = 0;
	int geklickterButton2 = 0;
    
    
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.settings);
        
        edStreet = (EditText) findViewById(R.id.edStreet);
        edCity = (EditText) findViewById(R.id.edCity);
        radioGroup = (RadioGroup) findViewById(R.id.radioGroup);
        radioGroup2 = (RadioGroup) findViewById(R.id.radioGroup2);
    	checkBox1 = (CheckBox) findViewById(R.id.checkBox1);
        
      //prefs laden
        SharedPreferences myPrefs = getSharedPreferences("mySharedSettings", 0 );
        edStreet.setText(myPrefs.getString("street", ""));
        edCity.setText(myPrefs.getString("city", "")); 
        //Checkbox laden
        if(myPrefs.getBoolean("checked", false)!= false)
        	checkBox1.setChecked(myPrefs.getBoolean("checked", false));
        //RadioGroup 1 laden
        geklickterButton = myPrefs.getInt("box", 0 );
        if(findViewById(geklickterButton) != null){
        	RadioButton ausgewaehlterButton = (RadioButton) findViewById(geklickterButton);
        	ausgewaehlterButton.setChecked(true);
        }
        //RadioGroup 2 laden
    	geklickterButton2 = myPrefs.getInt("box2", 0 );
        if(findViewById(geklickterButton2) != null){
        	RadioButton ausgewaehlterButton2 = (RadioButton) findViewById(geklickterButton2);
        	ausgewaehlterButton2.setChecked(true);
        }


        //Buttonlistener
        btnSave = (Button) findViewById(R.id.btnSave);
        btnSave.setOnClickListener(this);
        btnLoad = (Button) findViewById(R.id.btnLoad);
        btnLoad.setOnClickListener(this);
        
    }

	@Override
	public void onClick(View v) {
		

		
		if(v.equals(btnSave)){
			//hier wird gespeichert
			SharedPreferences myPrefs = getSharedPreferences("mySharedSettings", 0 );
			SharedPreferences.Editor prefsEdit = myPrefs.edit();
			prefsEdit.putString("street", edStreet.getText().toString());
			prefsEdit.putString("city", edCity.getText().toString());
			geklickterButton = radioGroup.getCheckedRadioButtonId();
			prefsEdit.putInt("box", geklickterButton);
			geklickterButton2 = radioGroup2.getCheckedRadioButtonId();
			prefsEdit.putInt("box2", geklickterButton2);
			prefsEdit.putBoolean("checked", checkBox1.isChecked());
			
			prefsEdit.commit();
		}
		if(v.equals(btnLoad)){
		      //prefs laden
	        SharedPreferences myPrefs = getSharedPreferences("mySharedSettings", 0 );
	        edStreet.setText(myPrefs.getString("street", ""));
	        edCity.setText(myPrefs.getString("city", ""));
	        
	        if(myPrefs.getInt("box", 0)!= 0 ){
	        	geklickterButton = myPrefs.getInt("box", 0 );
	        	RadioButton ausgewaehlterButton = (RadioButton) findViewById(geklickterButton);
	        	ausgewaehlterButton.setChecked(true);
	        }
	        
	        if(myPrefs.getInt("box2", 0)!= 0 ){
	        	geklickterButton2 = myPrefs.getInt("box2", 0 );
	        	RadioButton ausgewaehlterButton2 = (RadioButton) findViewById(geklickterButton2);
	        	ausgewaehlterButton2.setChecked(true);
	        }
		}
		
		
		
	}
}